package com.hwh.traffic.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hwh.traffic.MapApplication;
import com.hwh.traffic.R;
import com.hwh.traffic.busEntity.BusDomJson;
import com.hwh.traffic.busEntity.Routes;
import com.hwh.traffic.busEntity.Stops;
import com.hwh.traffic.db.TrafficLab;
import com.hwh.traffic.utils.BusApiUtil;
import com.hwh.traffic.utils.DensityUtil;
import com.hwh.traffic.utils.HttpUtil;

import java.io.IOException;
import java.util.List;

public class StopInfoActivity extends AppCompatActivity {



    private static ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    private BusDomJson stopInfo = null;
    private LatLng latLng;
    private TrafficLab trafficLab;
    private MapApplication mapApplication;


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_info);

        mapApplication = (MapApplication) getApplication();
        latLng = mapApplication.getLatLng();
        trafficLab = mapApplication.getTrafficLab();

        //加载动画
        View avi = findViewById(R.id.avi);

        ImageView backImg = findViewById(R.id.img_back);
        backImg.setOnClickListener(v -> this.finish());
        TextView title = findViewById(R.id.text_title);
        String bus_stop_api = getIntent().getStringExtra("STOP_API");
        String stopName = getIntent().getStringExtra("STOP_NAME");
        title.setText(stopName);
        new Thread(()->{
            try {
                String stopInfo_json = HttpUtil.httpGet(bus_stop_api);
                stopInfo = MAPPER.readValue(stopInfo_json, BusDomJson.class);
                //渲染数据
                runOnUiThread(()->{
                    getStopInfoList(stopInfo);
                    //停止加载动画
                    avi.setVisibility(View.GONE);
                });
            } catch (IOException e) {
                e.printStackTrace();
            }

        }).start();
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void getStopInfoList(BusDomJson stopInfo) {

        LinearLayout stop_info_list = findViewById(R.id.stop_info_list);
        List<Stops> stopsList = stopInfo.getItems().get(0).getStops();
        List<Routes> routes = stopsList.get(0).getRoutes();
        for (Routes route : routes) {
            //Route route = stops.getRoutes().get(0).getRoute();
            //Station station = stops.getRoutes().get(0).getStation();
            //List<Buses> buses = stops.getRoutes().get(0).getBuses();

            //显示一条信息

            //主线性布局
            LinearLayout mLinearLayout = new LinearLayout(getApplicationContext());

            //两个子线性布局 一个线性布局对应三个textview
            LinearLayout mLinearLayout_1 = new LinearLayout(getApplicationContext());
            TextView mTextView_stop_1 = new TextView(getApplicationContext());
            TextView mTextView_stop_2 = new TextView(getApplicationContext());
            TextView mTextView_stop_3 = new TextView(getApplicationContext());

            LinearLayout mLinearLayout_2 = new LinearLayout(getApplicationContext());
            TextView nTextView_stop_1 = new TextView(getApplicationContext());
            TextView nTextView_stop_2 = new TextView(getApplicationContext());
            TextView nTextView_stop_3 = new TextView(getApplicationContext());

            //需要先addView
            stop_info_list.addView(mLinearLayout);
            mLinearLayout.addView(mLinearLayout_1);
            mLinearLayout.addView(mLinearLayout_2);

            mLinearLayout_1.addView(mTextView_stop_1);
            mLinearLayout_1.addView(mTextView_stop_2);
            mLinearLayout_1.addView(mTextView_stop_3);

            mLinearLayout_2.addView(nTextView_stop_1);
            mLinearLayout_2.addView(nTextView_stop_2);
            mLinearLayout_2.addView(nTextView_stop_3);

            //设置mLinearLayout 布局
            LinearLayout.LayoutParams lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp_LinearLayout.width = ViewGroup.LayoutParams.MATCH_PARENT;
            lp_LinearLayout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            int dp_5 = DensityUtil.dip2px(getApplicationContext(), 5);
            int dp_8 = DensityUtil.dip2px(getApplicationContext(), 8);
            mLinearLayout.setPadding(dp_5,dp_5,dp_5,dp_5);
            //mLinearLayout.setLeftTopRightBottom(0,0,0,dp_8);
            lp_LinearLayout.setMargins(0,0,0,dp_8);
            mLinearLayout.setBackgroundColor(getResources().getColor(R.color.white));
            mLinearLayout.setLayoutParams(lp_LinearLayout);
            mLinearLayout.setOnClickListener(v -> {
                latLng = mapApplication.getLatLng();
                BusApiUtil.toBusInfoList(latLng,this,route.getRoute().getRouteName().replace("路",""),trafficLab);

            });

            //设置mLinearLayout_1 布局
            ViewGroup.LayoutParams lp_LinearLayout_1 = mLinearLayout_1.getLayoutParams();
            lp_LinearLayout_1.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp_LinearLayout_1.height = ViewGroup.LayoutParams.MATCH_PARENT;
            mLinearLayout_1.setOrientation(LinearLayout.VERTICAL);
            mLinearLayout_1.setLayoutParams(lp_LinearLayout_1);

            //设置三个textView mTextView_stop_1 mTextView_stop_2 mTextView_stop_3

            LinearLayout.LayoutParams lp_TextView_1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            lp_TextView_1.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp_TextView_1.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            mTextView_stop_1.setText(route.getRoute().getRouteName());
            mTextView_stop_1.setTextSize(17);
            mTextView_stop_1.setTextColor(getResources().getColor(R.color.black));
            mTextView_stop_1.setLayoutParams(lp_TextView_1);

            LinearLayout.LayoutParams lp_TextView_2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            lp_TextView_2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp_TextView_2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            mTextView_stop_2.setText(route.getRoute().getOrigin()+" → "+route.getRoute().getTerminal());
            mTextView_stop_2.setTextSize(12);
            mTextView_stop_2.setTextColor(getResources().getColor(R.color.Darkgrey));
            mTextView_stop_2.setLayoutParams(lp_TextView_2);

            LinearLayout.LayoutParams lp_TextView_3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            lp_TextView_3.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp_TextView_3.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            mTextView_stop_3.setText("首 "+route.getRoute().getFirstBus()+" 末 "+route.getRoute().getLastBus()+"  票价"+route.getRoute().getCarfare()+"元");
            mTextView_stop_3.setTextSize(12);
            mTextView_stop_3.setTextColor(getResources().getColor(R.color.grey));
            mTextView_stop_3.setLayoutParams(lp_TextView_3);


            //设置mLinearLayout_2
            ViewGroup.LayoutParams lp_LinearLayout_2 = mLinearLayout_2.getLayoutParams();
            lp_LinearLayout_2.width = ViewGroup.LayoutParams.MATCH_PARENT;
            lp_LinearLayout_2.height = ViewGroup.LayoutParams.MATCH_PARENT;
            mLinearLayout_2.setOrientation(LinearLayout.VERTICAL);
            mLinearLayout_2.setGravity(Gravity.RIGHT);
            mLinearLayout_2.setLayoutParams(lp_LinearLayout_2);

            //设置三个 textview nTextView_stop_1 nTextView_stop_2 nTextView_stop_3
            LinearLayout.LayoutParams nlp_TextView_1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            nlp_TextView_1.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            nlp_TextView_1.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            if (route.getBuses().size() != 0) {
                if (route.getBuses().get(0).getTargetStopCount() == 0){
                    nTextView_stop_1.setText("已到站");
                }
                else {
                    nTextView_stop_1.setText(route.getBuses().get(0).getTargetStopCount() + "站");
                }
            }else {
                nTextView_stop_1.setText("等待发车");
            }
            nTextView_stop_1.setTextSize(12);
            nTextView_stop_1.setTextColor(getResources().getColor(R.color.blue));
            nTextView_stop_1.setLayoutParams(nlp_TextView_1);

            LinearLayout.LayoutParams nlp_TextView_2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
            nlp_TextView_2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            nlp_TextView_2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            nTextView_stop_2.setText("最近一班距离");
            nTextView_stop_2.setTextSize(11);
            nTextView_stop_2.setTextColor(getResources().getColor(R.color.grey));
            nTextView_stop_2.setLayoutParams(nlp_TextView_2);

        }
    }
}
